package com.ruoyi.service.service.impl;

import com.ruoyi.service.domain.ServiceOrders;
import com.ruoyi.service.mapper.ServiceOrdersMapper;
import com.ruoyi.service.service.IServiceOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 订单信息表Service业务层处理
 * 
 * @author ZDL
 * @date 2024-11-13
 */
@Service
public class ServiceOrdersServiceImpl implements IServiceOrdersService 
{
    @Autowired
    private ServiceOrdersMapper serviceOrdersMapper;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final AtomicInteger sequence = new AtomicInteger(0);

    /**
     * 查询订单信息表
     * 
     * @param orderId 订单信息表主键
     * @return 订单信息表
     */
    @Override
    public ServiceOrders selectServiceOrdersByOrderId(Long orderId)
    {
        return serviceOrdersMapper.selectServiceOrdersByOrderId(orderId);
    }

    @Override
    public ServiceOrders selectServiceOrdersByOrderCode(String orderCode)
    {
        return serviceOrdersMapper.selectServiceOrdersByOrderCode(orderCode);
    }

    /**
     * 查询订单信息表列表
     * 
     * @param serviceOrders 订单信息表
     * @return 订单信息表
     */
    @Override
    public List<ServiceOrders> selectServiceOrdersList(ServiceOrders serviceOrders)
    {
        return serviceOrdersMapper.selectServiceOrdersList(serviceOrders);
    }

    @Override
    public List<ServiceOrders> selectServiceOrdersListAll(ServiceOrders serviceOrders)
    {
        return serviceOrdersMapper.selectServiceOrdersList(serviceOrders);
    }

    /**
     * 新增订单信息表
     * 
     * @param serviceOrders 订单信息表
     * @return 结果
     */
    @Override
    public int insertServiceOrders(ServiceOrders serviceOrders)
    {
        serviceOrders.setOrderCode(generate());
        serviceOrders.setOrderCreatetime(new Date());
        serviceOrders.setOrderUserid(getUserId());
        serviceOrders.setOrderServiceflag(1);
        return serviceOrdersMapper.insertServiceOrders(serviceOrders);
    }

    /**
     * 修改订单信息表
     * 
     * @param serviceOrders 订单信息表
     * @return 结果
     */
    @Override
    public int updateServiceOrders(ServiceOrders serviceOrders)
    {
        return serviceOrdersMapper.updateServiceOrders(serviceOrders);
    }

    /**
     * 批量删除订单信息表
     * 
     * @param orderIds 需要删除的订单信息表主键
     * @return 结果
     */
    @Override
    public int deleteServiceOrdersByOrderIds(Long[] orderIds)
    {
        return serviceOrdersMapper.deleteServiceOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单信息表信息
     * 
     * @param orderId 订单信息表主键
     * @return 结果
     */
    @Override
    public int deleteServiceOrdersByOrderId(Long orderId)
    {
        return serviceOrdersMapper.deleteServiceOrdersByOrderId(orderId);
    }

    public static String generate()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(dateFormat.format(new Date()));
        sb.append(String.format("%04d", sequence.getAndIncrement()));
        return sb.toString();
    }

}
